package com.ljx.recruitment.interview.event.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ljx.recruitment.common.enums.CommonStatusTypeEnum;
import com.ljx.recruitment.interview.constant.JobConstant;
import com.ljx.recruitment.interview.domain.entity.Job;
import com.ljx.recruitment.interview.domain.entity.JobLocation;
import com.ljx.recruitment.interview.domain.entity.JobProcess;
import com.ljx.recruitment.interview.enums.DeliverStatusEnum;
import com.ljx.recruitment.interview.enums.JobBaseProcessEnum;
import com.ljx.recruitment.interview.event.JobSubmitEvent;
import com.ljx.recruitment.interview.mapper.JobMapper;
import com.ljx.recruitment.interview.service.JobLocationService;
import com.ljx.recruitment.interview.service.JobProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-11-09  14:40
 */
@Component
public class JobSubmitEventImpl {

    @Autowired
    private JobMapper jobMapper;

    @Autowired
    private JobProcessService jobProcessService;

//    @Async("asyncExecutor")
//    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = JobSubmitEvent.class, fallbackExecution = true)
//    public void saveJobLocation(JobSubmitEvent jobSubmitEvent){
//        Job job = jobMapper.selectById(jobSubmitEvent.getJobId());
//        List<String> locations = job.getJobLocationDetails();
//        List<JobLocation> jobLocations = jobLocationService.list(new LambdaQueryWrapper<JobLocation>()
//                .eq(JobLocation::getTenantId, job.getTenantId())
//                .in(JobLocation::getName, locations));
//        List<Long> jobLocationIds = jobLocations.stream().map(JobLocation::getId).collect(Collectors.toList());
//        jobMapper.removeJobLocation(job.getId());
//        jobMapper.batchSaveJobLocation(job.getId(), jobLocationIds);
//    }

    @Async("asyncExecutor")
    @TransactionalEventListener(phase = TransactionPhase.BEFORE_COMMIT, classes = JobSubmitEvent.class, fallbackExecution = true)
    public void initJobProcess(JobSubmitEvent jobSubmitEvent){
        Job job = jobMapper.selectById(jobSubmitEvent.getJobId());
        List<JobProcess> jobBaseProcessSort = Arrays.stream(JobBaseProcessEnum.values())
                .sorted(Comparator.comparingInt(JobBaseProcessEnum::getKey))
                .map(item -> {
                    JobProcess jobProcess = new JobProcess();
                    jobProcess.setJobId(job.getId());
                    jobProcess.setProcessKey(item.getKey());
                    jobProcess.setProcessName(item.getValue() + JobConstant.DEFAULT_PROCESS_BATCH);
                    jobProcess.setBatch(JobConstant.DEFAULT_PROCESS_BATCH);
                    return jobProcess;
                }).collect(Collectors.toList());
        jobProcessService.saveBatch(jobBaseProcessSort);
    }
}
